package com.yuyou.fn.timer.task;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.electronicinvoice.entity.Invoice;
import com.yuyou.fn.electronicinvoice.entity.RecordTempInvoice;
import com.yuyou.fn.electronicinvoice.service.IFinanceSubjectService;
import com.yuyou.fn.electronicinvoice.service.IInvoiceService;
import com.yuyou.fn.electronicinvoice.service.IRecordTempInvoiceService;
import com.yuyou.fn.electronicinvoice.vo.InvoiceSaveVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.rmi.RemoteException;
import java.util.List;

@JobHander(value="DealFailInvoiceTask")
@Component
public class DealFailInvoiceTask extends IJobHandler {

    @Resource
    private IRecordTempInvoiceService recordTempInvoiceService;

    @Resource
    private IInvoiceService invoiceService;

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IFinanceSubjectService financeSubjectService;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        List<RecordTempInvoice> recordTempInvoiceList=recordTempInvoiceService.findUnDealRecordOrderByInvoiceType();
        boolean hasFail = false ;
        if (recordTempInvoiceList!=null){
            for (RecordTempInvoice recordTempInvoice:recordTempInvoiceList){
                try{
                    handle(recordTempInvoice);
                }catch (Exception e ){
                    hasFail = true ;
                    XxlJobLogger.log("处理失败:recordTempInvoiceId={0},error={1}" , recordTempInvoice.getRecordTempInvoiceId(),Utils.getExceptionStackTrace(e));
                }
            }

        }


        return hasFail ?  ReturnT.FAIL : ReturnT.SUCCESS;
    }

    private void handle(RecordTempInvoice recordTempInvoice) throws RemoteException {
        if ("invoiceId".equals(recordTempInvoice.getObjectType()) && recordTempInvoice.getInvoiceType()==2){
            Invoice invoice=invoiceService.findById(recordTempInvoice.getObjectId());
            InvoiceSaveVo invoiceSaveVo=new InvoiceSaveVo();
            BeanUtils.copyProperties(invoiceSaveVo,invoice);
            invoiceSaveVo.setInvoiceId(recordTempInvoice.getRecordTempInvoiceId());
            invoiceSaveVo.setRushInvoiceId(invoice.getInvoiceId());
            invoiceSaveVo.setInvoiceType(2);
            invoiceSaveVo.setOperationCode(21);
            Invoice rushInvoice=invoiceService.saveOrRushInvoice(invoiceSaveVo);
        }else if ("regId".equals(recordTempInvoice.getObjectType()) && recordTempInvoice.getInvoiceType()==1){
            StudentReg studentReg=studentRegService.findById(recordTempInvoice.getObjectId());
            InvoiceSaveVo invoiceSaveVo=invoiceService.openInvoice(studentReg.getRegId(),1,"regId");

            Invoice newInvoice=invoiceService.saveOrRushInvoice(invoiceSaveVo);
        }
    }


}
